// FXDialog.cpp : implementation file
//

#include "stdafx.h"
#include <d3dx9.h>
#include <D3D9.h>

#include "dxutil.h"
#include "d3dutil.h"
#include "d3dfont.h"
#include "D3DEnumeration.h"
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "fxtool.h"
#include "FXDialog.h"
#include <AniContainer.h>
#include "Effect.h"
#include "MainFrm.h"
#include <D3DModel.h>
#include "dxutil.h"
#include "Trails.h"
#include "ToolApp.h"

extern SDrawElement* g_pActivityMesh;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFXDialog dialog
extern D3DModel *GetModel();

CFXDialog::CFXDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CFXDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFXDialog)
	m_WorldVecX = 0.0f;
	m_WorldVecY = 0.0f;
	m_WorldVecZ = 0.0f;
	//}}AFX_DATA_INIT
}


void CFXDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFXDialog)
	DDX_Text(pDX, IDC_WORLDVEC_X, m_WorldVecX);
	DDX_Text(pDX, IDC_WORLDVEC_Y, m_WorldVecY);
	DDX_Text(pDX, IDC_WORLDVEC_Z, m_WorldVecZ);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFXDialog, CDialog)
	//{{AFX_MSG_MAP(CFXDialog)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFXDialog message handlers

BOOL CFXDialog::Create(CWnd* pParentWnd) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::Create(IDD, pParentWnd);
}

void CFXDialog::GetValue()
{
	int		slotpos = m_EffectClass->m_CurrentPos;
		
	m_WorldVecX = m_EffectClass->m_Effect[slotpos].Plane[0].WorldVec.x;
	m_WorldVecY = m_EffectClass->m_Effect[slotpos].Plane[0].WorldVec.y;
	m_WorldVecZ = m_EffectClass->m_Effect[slotpos].Plane[0].WorldVec.z;

	UpdateData(FALSE);
}

void CFXDialog::SetValue()
{
	D3DXVECTOR3			vector;

	int		slotpos = m_EffectClass->GetFunction();

	(m_EffectClass->*m_EffectClass->InitFunc) ( );	

	UpdateData(TRUE);

	for( int i=0; i<MAX_EFFECTPLANE; i++ )
	{
		if( m_EffectClass->m_Effect[slotpos].Plane[i].Activate )
		{
			m_EffectClass->m_Effect[slotpos].Plane[i].WorldVec.x = m_WorldVecX;
			m_EffectClass->m_Effect[slotpos].Plane[i].WorldVec.y = m_WorldVecY;
			m_EffectClass->m_Effect[slotpos].Plane[i].WorldVec.z = m_WorldVecZ;
		}
	}	
	
	D3DXVec3TransformCoord( &vector, &m_EffectClass->m_Effect[slotpos].Plane[0].WorldVec, &m_EffectClass->m_ViewMat );
		
	m_EffectClass->m_Effect[slotpos].Plane[0].WorldMat._41 = vector.x;
	m_EffectClass->m_Effect[slotpos].Plane[0].WorldMat._42 = vector.y;
	m_EffectClass->m_Effect[slotpos].Plane[0].WorldMat._43 = vector.z;	
}

void CFXDialog::OnButtonSave() 
{
	// TODO: Add your control notification handler code here

	SetValue();
	SetTimer( 0, 1, NULL );	
	if (GetModel())
	{
		DXUtil_Timer( TIMER_START );
	}
}

void CFXDialog::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	for( int i=0; i<MAX_EFFECTSLOT; i++ )
	{
		if( m_EffectClass->m_Effect[i].IsSwitchOn == FALSE )		continue;

		(m_EffectClass->*m_EffectClass->m_Effect[i].SetFunc) ( i );
	}

//	Trails::MakeTrails(g_pActivityMesh,5);
	((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();

	bool		flag = false;
	for(int i=0; i<MAX_EFFECTSLOT; i++ )
		if( m_EffectClass->m_Effect[i].IsSwitchOn == TRUE )		flag = true;
	int aniflag=0;
	//if (GetModel())aniflag=GetModel()->FrameMove();
	
	if( !flag  && aniflag)
	{
		DXUtil_Timer( TIMER_STOP );
		KillTimer( 0 );
	}
	
	CDialog::OnTimer(nIDEvent);
}
